package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Spaqrz;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class SpaqrzForm extends BasePage implements Serializable {
    private GenericManager<Spaqrz, Long> spaqrzManager;
    private Spaqrz spaqrz = new Spaqrz();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setSpaqrzManager(GenericManager<Spaqrz, Long> spaqrzManager) {
        this.spaqrzManager = spaqrzManager;
    }

    public Spaqrz getSpaqrz() {
        return spaqrz;
    }

    public void setSpaqrz(Spaqrz spaqrz) {
        this.spaqrz = spaqrz;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        spaqrzManager.remove(spaqrz.getId());
        addMessage("spaqrz.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            spaqrz = spaqrzManager.get(id);
        } else {
            spaqrz = new Spaqrz();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (spaqrz.getId() == null || spaqrz.getId() == 0);
        spaqrz.setJbqk( jbqk );
        spaqrz.setUsername(getRequest().getRemoteUser());
        spaqrz.setOptdate(new Date());
        spaqrz.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        spaqrzManager.save(spaqrz);

        String key = (isNew) ? "spaqrz.added" : "spaqrz.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = spaqrz.getId();
            return "edit";
        }
    }
} 